@echo off
SETLOCAL
(set WF=)

if "%1"=="" goto :usage
if "%1"=="/?" goto :usage
if "%1"=="--help" goto :usage
if "%1"=="-h" goto :usage

:: Look for file in the current directory
  for %%a in ("" %PATHEXT:;= %) do (
     if not defined WF if exist "%~1%%~a" set WF=%CD%\%~1%%~a)

:: Look for file in the PATH
  for %%a in ("" %PATHEXT:;= %) do (
     if not defined WF for %%g in ("%~1%%~a") do (
        if exist "%%~$PATH:g" set WF=%%~$PATH:g))

:: Store the Help Error message
  if NOT "%~x1"=="" goto:END
  for /f "delims=." %%a in ('help.exe *') do set HlpErr=%%a

:: If command missing from Help, goto end
  help.exe "%~1" |find "%HlpErr%">nul && goto:END

:: Listed in help so find file in System32
  for %%a in (exe com) do (
     if /i "%WF%"=="%windir%\system32\%~1.%%a" goto:END)

:: File not in System32, so must be an internal command
  set WF="%~1" is an internal command

  :END
  if defined WF (echo.%WF%) else (
     echo The file: "%~1" was not found) 
   
  goto :eof
:: Credits:
:: Clay Calvert 
:: alt.msdos.batch.nt

:usage
echo which returns the file which will be called by cmd for a given command.
echo For example, "which cmd" gives
which cmd